package definite.Guide.Book.JFileChooser;

import java.io.File;

import javax.swing.filechooser.FileFilter;

public class CustomFileFilter extends FileFilter {

	private String extensions[];
	private String description;

	public CustomFileFilter(String description, String extensions[]) {
		if (description == null)
			this.description = extensions[0] + " [" + extensions.length + " ]";
		else
			this.description = description;
		this.extensions = extensions.clone();
		toLower(this.extensions);
	}

	// ignore case , alway accept directory
	@Override
	public boolean accept(File f) {
		if (f.isDirectory())
			return true;
		String path = f.getAbsolutePath().toLowerCase();
		for (int i = 0; i < extensions.length; i++) {
			String str = extensions[i];
			if (path.endsWith(str)
				&& (path.charAt(path.length() - str.length() - 1) == '.'))
				return true;
		}
		return false;
	}

	@Override
	public String getDescription() {

		return description;
	}

	private void toLower(String[] array) {
		for (int i = 0; i < array.length; i++)
			array[i] = array[i].toLowerCase();
	}
}
